import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {NzMessageService} from 'ng-zorro-antd/message';

@Injectable({
  providedIn: 'root'
})
export class AisiPriceService {
  serviceUrl = {
    quoteList: '/hqbQuotation/list', // 获取报价单列表
    addAndEditQuote: '/hqbQuotation/saveOrUpdate', // 新增和修改报价单
    refreshQuote: '/hqbQuotation/updateStrikePrice', // 更新报价单
    dataConfig: '/hqbType/list', // 报价数据配置列表
    addAndEditCategory: '/hqbType/saveOrUpdateType', // 新增更新分类
    brandAndSeries: '/hqbType/getBannerAndSeries', // 获取所有品牌和系列
    getBrandBySeries: 'hqbType/getBannerNameBySeriesId', // 根据系列id获取品牌 seriesId=8
    quote: '/quote/index', // 报价查询
    fileUpload: '/common/upload/file', // 文件上传
  };

  constructor(
    private httpClient: HttpClient,
    private nzMessageService: NzMessageService
  ) {
  }

  // 文件上传
  fileUpload(file: any) {
    return new Promise((resolve, reject) => {
      if (!file) {
        return resolve('');
      }
      const fileData = new FormData();
      fileData.append('file', file);
      fileData.append('name', (+new Date()).toString());
      this.httpPost(this.serviceUrl.fileUpload, fileData).then((res: any) => {
        if (res.code === 0) {
          resolve(res.data);
        } else {
          this.nzMessageService.error('文件上传失败');
          resolve('');
        }
      }).catch(() => {
        this.nzMessageService.error('文件上传失败');
        resolve('');
      });
    });
  }


  httpGet(url: string, params: any) {
    return this.httpClient.get(url, {params}).toPromise();
  }

  httpPost(url: string, body: any) {
    // return this.httpClient.post(url, body).toPromise().then((res: any) => {
    //   return res.code === 0 ? res.data : {current: 0, pages: 0, records: [], searchCount: 0, size: 0, total: 0};
    // });
    return this.httpClient.post(url, body).toPromise();
  }
}
